﻿using System;
using System.Web;
/// <summary>
/// Request请求常用方法
/// </summary>
public static class ReadRequest
{
    /// <summary>
    /// 根据key获取string类型参数
    /// </summary>
    /// <param name="key">参数名称</param>
    /// <returns></returns>
    public static string RequestString(string key)
    {
        object req = HttpContext.Current.Request[key];
        if (req == null)
        {
            return "";
        }
        else
        {
            if (string.IsNullOrEmpty(req.ToString()))
            {
                return "";
            }
            else
            {
                return URL.UrlDecode(req.ToString());
            }
        }
    }
    /// <summary>
    /// 根据key获取string类型参数
    /// </summary>
    /// <param name="key">参数名称</param>
    /// <returns></returns>
    public static string RequestHtmlEnCode(string key)
    {
        object req = HttpContext.Current.Request[key];
        if (req == null)
        {
            return "";
        }
        else
        {
            if (string.IsNullOrEmpty(req.ToString()))
            {
                return "";
            }
            else
            {
                return URL.HTMLEncode(URL.UrlDecode(req.ToString()));
            }
        }
    }
    /// <summary>
    /// 根据key获取Int类型参数
    /// </summary>
    /// <param name="key">参数名称</param>
    /// <returns></returns>
    public static int RequestInt(string key)
    {
        object req = HttpContext.Current.Request[key];
        if (req == null)
        {
            return 0;
        }
        else
        {
            int result = 0;
            int.TryParse(req.ToString(), out result);
            return result;
        }
    }

    /// <summary>
    /// 根据key获取long类型参数
    /// </summary>
    /// <param name="key">参数名称</param>
    /// <returns></returns>
    public static long RequestLong(string key)
    {
        object req = HttpContext.Current.Request[key];
        if (req == null)
        {
            return 0;
        }
        else
        {
            long result = 0;
            long.TryParse(req.ToString(), out result);
            return result;
        }
    }
    public static decimal Requestdecimal(string key)
    {
        object req = HttpContext.Current.Request[key];
        if (req == null)
        {
            return 0m;
        }
        else
        {
            try
            {               
               return Convert.ToDecimal(req.ToString());
            }
            catch
            { 
            
            }
            return 0m;
        }
    }
    /// <summary>
    /// 获取用户真实IP地址
    /// </summary>
    /// <returns>IP地址</returns>
    public static string getUserRealIP()
    {
        string ip = "";
        try
        {
            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
            }
            else
            {
                ip = HttpContext.Current.Request.UserHostAddress;

            }
        }
        catch (Exception e)
        {
            throw e;
        }
        if (ip == null)
            ip = "";
        return ip;
    }
}

